package com.crm.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jws.Oneway;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.crm.entity.Asker;
import com.crm.entity.Modules;
import com.crm.entity.OpenClose;
import com.crm.entity.Student;
import com.crm.service.NetFollowService;
import com.crm.service.OpenCloseService;
import com.crm.service.StudentService;
import com.crm.util.Result;

//学生的控制层
@RestController
@RequestMapping("/student")
public class StudentController {
	@Autowired
	private StudentService studentService;// 自动注入
	@Autowired
	private OpenCloseService openCloseService;
	@Autowired
	private NetFollowService netFollowService;// 自动注入
		
//	public ArrayList<Asker> list=studentService.getAsker(asker);
	private List<Asker> list = new ArrayList<>();
			
	/**
	 * 我的学生的多条件查询
	 * 
	 * @param map
	 * @param page
	 * @param limit
	 * @return
	 */
	@RequestMapping("/getAllStudent")
	public Object getAllStudent(@RequestParam Map<String, Object> map, @RequestParam int page, @RequestParam int limit,
			String loginName) {
		map.put("page", (page - 1) * limit);
		map.put("limit", limit);
		if (studentService.getLoginName(loginName) == 2) {// 是咨询师 根据咨询师去查询
			map.put("askerNamess", loginName);
			Result result = new Result("0", "true", studentService.getAllStudentCount(map),
					studentService.getAllStudent(map));
			return result;
		} else if (studentService.getLoginName(loginName) == 3) {// 是咨询经理，不传值
			Result result = new Result("0", "true", studentService.getAllStudentCount(map),
					studentService.getAllStudent(map));
			return result;
		}
		return null;
	}

	/**
	 * 我的学生的选中导出
	 * 
	 * @param map
	 * @param page
	 * @param limit
	 * @return
	 */
	@RequestMapping("/getAllStudentExcel")
	public Object getAllStudentExcel(@RequestParam Map<String, Object> map,
			 @RequestParam("ids") String ids,  String loginName) {
		if (studentService.getLoginName(loginName) == 2) {// 是咨询师 根据咨询师去查询
			map.put("askerNamess", loginName);
			if (ids!=null) {
				String str[]=ids.split(",");//用,来分割转换成数组
				map.put("id", str);
			}
			Result result = new Result("0", "导出成功", 1000, studentService.getAllStudentExcel(map));
			return result;
		} else if (studentService.getLoginName(loginName) == 3) {// 是咨询经理，不传值
			String str[]=ids.split(",");//用,来分割转换成数组
			map.put("id", str);
			Result result=new Result("0", "导出成功", 1000,studentService.getAllStudentExcel(map));
			return result;
		}
		return null;
	}
	/**
	 * 我的学生的多条件查询导出
	 * 
	 * @param map
	 * @param page
	 * @param limit
	 * @return
	 */
	@RequestMapping("/getAllStudentE")
	public Object getAllStudentE(@RequestParam Map<String, Object> map,  String loginName) {
		if (studentService.getLoginName(loginName) == 2) {// 是咨询师 根据咨询师去查询
			map.put("askerNamess", loginName);
			Result result = new Result("0", "导出成功", 1000, studentService.getAllStudentExcel(map));
			return result;
		} else if (studentService.getLoginName(loginName) == 3) {// 是咨询经理，不传值
			Result result=new Result("0", "导出成功", 1000,studentService.getAllStudentExcel(map));
			return result;
		}
		return null;
	}

	/**
	 * 网络学生的多条件查询
	 * 
	 * @param map
	 * @param page
	 * @param limit
	 * @return
	 */
	@RequestMapping("/getAllStudetnNet")
	public Object getAllStudetnNet(@RequestParam Map<String, Object> map, @RequestParam int page,
			@RequestParam int limit, String loginName) {
		map.put("page", (page - 1) * limit);
		map.put("limit", limit);
		if (studentService.getLoginName(loginName) == 4) {// 是网络咨询 根据网络咨询姓名去查询
			map.put("netPusherIdss", loginName);
			Result result = new Result("0", "true", studentService.getAllStudetnNetCount(map),
					studentService.getAllStudetnNet(map));
			return result;
		} else if (studentService.getLoginName(loginName) == 2) {// 是咨询师 根据咨询师去查询
			map.put("askerNamess", loginName);
			Result result = new Result("0", "true", studentService.getAllStudetnNetCount(map),
					studentService.getAllStudetnNet(map));
			return result;
		} else if (studentService.getLoginName(loginName) == 3) {// 是咨询经理，不传值
			Result result = new Result("0", "true", studentService.getAllStudetnNetCount(map),
					studentService.getAllStudetnNet(map));
			return result;
		}
		return null;
	}

	/**
	 * 网络学生的选中导出
	 * 
	 * @param map
	 * @param page
	 * @param limit
	 * @return
	 */
	@RequestMapping("/getAllStudetnNetExcel")
	public Object getAllStudetnNetExcel(@RequestParam Map<String, Object> map, String loginName,@RequestParam("ids") String ids) {
		if (studentService.getLoginName(loginName) == 4) {// 是网络咨询 根据网络咨询姓名去查询
			map.put("netPusherIdss", loginName);
			System.out.println(loginName);
			if (ids!=null) {
				String str[]=ids.split(",");//用,来分割转换成数组
				map.put("id", str);
			}
			Result result = new Result("0", "导出成功", 1000, studentService.getAllStudetnNetExcel(map));
			return result;
		} else if (studentService.getLoginName(loginName) == 2) {// 是咨询师 根据咨询师去查询
			map.put("askerNamess", loginName);
			if (ids!=null) {
				String str[]=ids.split(",");//用,来分割转换成数组
				map.put("id", str);
			}
			Result result = new Result("0", "导出成功", 1000, studentService.getAllStudetnNetExcel(map));
			return result;
		} else if (studentService.getLoginName(loginName) == 3) {// 是咨询经理，不传值
			if (ids!=null) {
				String str[]=ids.split(",");//用,来分割转换成数组
				map.put("id", str);
			}
			Result result = new Result("0", "导出成功", 1000, studentService.getAllStudetnNetExcel(map));
			return result;
		}
		return null;
	}
	/**
	 * 网络学生的多条件查询导出
	 * 
	 * @param map
	 * @param page
	 * @param limit
	 * @return
	 */
	@RequestMapping("/getAllStudetnNetE")
	public Object getAllStudetnNetE(@RequestParam Map<String, Object> map, String loginName) {
		if (studentService.getLoginName(loginName) == 4) {// 是网络咨询 根据网络咨询姓名去查询
			map.put("netPusherIdss", loginName);
			Result result = new Result("0", "导出成功", 1000,
					studentService.getAllStudetnNetExcel(map));
			return result;
		} else if (studentService.getLoginName(loginName) == 2) {// 是咨询师 根据咨询师去查询
			map.put("askerNamess", loginName);
			Result result = new Result("0", "导出成功", 1000,
					studentService.getAllStudetnNetExcel(map));
			return result;
		} else if (studentService.getLoginName(loginName) == 3) {// 是咨询经理，不传值
			Result result = new Result("0", "导出成功", 1000,
					studentService.getAllStudetnNetExcel(map));
			return result;
		}
		return null;
	}

	/**
	 * 添加学生 查询自动分配是否打开 1; //打开 允许
	 * 
	 * @param student
	 * @return
	 */
	@RequestMapping("/add")
	public Object add(Student student, int isopens) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		student.setIsValid("是");// 是否有效
		student.setCreateTime(df.format(new Date()));// 创建日期
		student.setIsReturnVist("否");// 是否回访
		student.setIsPay("是");// 是否缴费
		student.setPayTime(df.format(new Date()));// 缴费时间
		student.setIsInClass("是");// 是否进班
		student.setInClassTime(df.format(new Date()));// 进班时间
		student.setIsDel("否");// 是否删除
		student.setIsBaoBei("是");// 是否报备
		if (isopens == 1) {// 查询自动分配是否打开 1; //打开 允许
			if(list == null ||   list.size()==0) {//判断list是否为空
				list=studentService.getAsker(new Asker());//把查询到的数据赋给list
			}
			Asker asker2= list.get(0);//拿到第一条数据
			student.setAskerid(asker2.getAskerId());
			student.setExOne("是");
			list.remove(0);//移除第一条数据
			Result result = new Result("200", "true", studentService.add(student), "后台请求成功");
			return result;
		} else {
			student.setExOne("否");
			Result result = new Result("200", "true", studentService.add(student), "后台请求成功");
			return result;
		}
	}

	/**
	 * 删除学生信息，把isDel改成是
	 * 
	 * @param student
	 * @return
	 */
	@RequestMapping("/updateisDel")
	public Object updateisDel(Student student) {
		student.setIsDel("是");// 是否删除
		Result result = new Result("200", "true", studentService.updateisDel(student), "后台请求成功");
		return result;
	}
	/**
	 * 网络学生删除学生信息，把isDel改成是
	 * 
	 * @param student
	 * @return
	 */
	@RequestMapping("/updateisDelNet")
	public Object updateisDelNet(Student student,int id) {
		if (studentService.selectAsker(id)!=0) {//判断是否分配咨询师!=0分配咨询师不可以删除
			return  new Result("200", "分配有咨询师，不可以删除", 0, "后台请求失败");
		}
		student.setIsDel("是");// 是否删除
		Result result = new Result("200", "true", studentService.updateisDel(student), "后台请求成功");
		return result;
	}
	/**
	 * 根据学生id去修改咨询师id 批量修改咨询师
	 * 
	 * @param student
	 * @return
	 */
	@RequestMapping("/updateisAskerId")
	public Object updateisAskerId(Student student,@RequestParam("ids") Object ids,int askerId) {
		JSONArray js = JSON.parseArray(ids.toString());//将从ids.toString()中获得的字符串直接转换成对象
		Integer jg = 0;
		for (Object i : js) {
			student.setId(Integer.parseInt(i.toString()));
			student.setAskerid(askerId);
			student.setExOne("是");
			jg += studentService.updateisAskerId(student);
		}
		if (jg == js.size()) {
			return new Result("0", "修改成功", jg, "后台请求成功");
		}
		Result result=new Result("0", "修改失败", 0, "后台请求成功");
		return result;
	}

	/**
	 * 编辑学生信息
	 * 
	 * @param student
	 * @return
	 */
	@RequestMapping("/update")
	public Object update(Student student) {
		Result result = new Result("200", "true", studentService.update(student), "后台请求成功");
		return result;
	}
	/**
	 * 根据角色名称去查询是否给显示批量修改咨询师按钮
	 * @param loginName
	 * @return
	 */
	@RequestMapping("/getLoginName")
	public Object getLoginName(String loginName) {
		if (studentService.getLoginName(loginName)==3) {//是咨询师
			Result result = new Result("0", "true",100 , "后台请求成功");
			return result;
		}
		return null;
	}
}
